package com.china08.yxyapi.api.manage.tec.teaching.homework.myques;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyYxyQuesResource;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroupRelQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRelQuesRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;
import com.china08.yxyapi.service.mg.HomeworkQuesGroupService;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class HomeworkSwitchoverGroupController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuesGroupRelQuesRepository homeworkQuesGroupRelQuesRepository;

    @Autowired
    private ElasticQuesService elasticQuesService;

    @Autowired
    private HomeworkQuesGroupService homeworkQuesGroupService;

    @ApiOperation(value = "试题切换分组")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/myques/ques/switchover/group", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        String teacherId = profile.getId();
        List<String> quesList = reqModel.quesId;
        for (String string : quesList) {
            ElasticYxyYxyQuesResource elasticYxyYxyQuesResource = elasticQuesService.findByEsQuesId(string);
            Assert.notNull(elasticYxyYxyQuesResource, "试题找不到了请刷新页面");
            if (StringUtils.equals("1", reqModel.orginType)) {
                // ## 我的收藏移动到其他分组
                HomeworkQuesGroupRelQues groupRelQues = new HomeworkQuesGroupRelQues();
                groupRelQues.setTeacherId(teacherId);
                groupRelQues.setSchoolId(schoolId);
                groupRelQues.setStage(reqModel.phase);
                groupRelQues.setSubject(reqModel.subject);
                groupRelQues.setQuesId(string);
                groupRelQues.setGroupId(reqModel.targetGroupId);
                groupRelQues.setQuesType(elasticYxyYxyQuesResource.getQuestionType());
                groupRelQues.setSrcType(elasticYxyYxyQuesResource.getSrcType());
                groupRelQues.setDifficultyLevel(elasticYxyYxyQuesResource.getDifficultyLevel() != null
                        ? Integer.parseInt(elasticYxyYxyQuesResource.getDifficultyLevel()) : null);
                groupRelQues.setCreatedBy(teacherId);
                groupRelQues.setLastModifiedBy(teacherId);
                homeworkQuesGroupRelQuesRepository.save(groupRelQues);
            } else {
                HomeworkQuesGroupRelQues groupRelQues = homeworkQuesGroupRelQuesRepository
                        .findByGroupIdAndQuesIdAndTeacherIdAndSchoolIdAndStageAndSubject(reqModel.orginType, string,
                                teacherId, schoolId, reqModel.phase, reqModel.subject);
                if (groupRelQues != null) {
                    groupRelQues.setGroupId(reqModel.targetGroupId);
                    homeworkQuesGroupRelQuesRepository.save(groupRelQues);
                }
            }
        }
        if (!StringUtils.equals("myques", reqModel.orginType)) {
            homeworkQuesGroupService.updateQuesCount(reqModel.orginType);
        }

        if (!StringUtils.equals("myques", reqModel.targetGroupId)) {
            homeworkQuesGroupService.updateQuesCount(reqModel.targetGroupId);
        }

        return new YxyApiRespModel("移动成功");
    }

    protected static class ReqModel extends YxyApiReqModel {
        public List<String> quesId;
        // 3我的收藏 其他的是组的Id
        public String orginType;

        public String targetGroupId;

        public String phase;

        public String subject;

        @Override
        public void autoCheck() {
            Assert.isTrue(quesId != null && quesId.size() > 0, "请选择要移动的试题");
            Assert.notNull(StringUtils.defaultIfBlank(orginType, null), "请填写来源");
            Assert.notNull(StringUtils.defaultIfBlank(targetGroupId, null), "请填写要移动的分组");
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请填写学段");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "请填写学科");
        }
    }

}
